﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 文章仓储
/// </summary>
public class MdArticleRep : BaseRep<MdArticleModel>
{
    private static string sqlFields = string.Format(@"
        `article_id`,
        `title`,
        `sub_title`,
        `seo_title`,
        `seo_desc`,
        `seo_keywork`,
        `cover`,
        `thum_cover`,
        `description`,
        `content`,
        `author`,
        `tag`,
        `files`,
        `video_files`,
        `link`,
        `is_external_link`,
        `category_id`,
        `article_type`,
        `status`,
        `del_flag`,
        `create_by`,
        `create_time`,
        `update_by`,
        `update_time`
    ");

    /// <summary>
    /// 通过编号获取文章详情
    /// </summary>
    /// <param name="id">文章编号</param>
    /// <returns>MdArticleModel</returns>
    public MdArticleModel? GetMdArticleById(long id)
    {
        string sqlWhere = string.Format(@" and `del_flag` = '0' and `article_id` = @ArticleId ");

        return QuerySingle(sqlFields, sqlWhere, new { ArticleId = id });
    }

    /// <summary>
    /// 获取文章列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒叙</param>
    /// <returns>IEnumerable<MdArticleModel></returns>
    public IEnumerable<MdArticleModel> GetMdArticleList(MdArticleModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Title))
        {
            sqlWhere += " and `title` like concat('%', @Title, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.SubTitle))
        {
            sqlWhere += " and `sub_title` like concat('%', @SubTitle, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.SeoTitle))
        {
            sqlWhere += " and `seo_title` like concat('%', @SeoTitle, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Author))
        {
            sqlWhere += " and `author` like concat('%', @Author, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.IsExternalLink))
        {
            sqlWhere += " and `is_external_link` = @IsExternalLink ";
        }
        if (model.CategoryId > 0)
        {
            sqlWhere += " and `category_id` = @CategoryId ";
        }
        if (!string.IsNullOrWhiteSpace(model.ArticleType))
        {
            sqlWhere += " and `article_type` = @ArticleType ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }
        sqlWhere += " and `del_flag` = '0' ";
        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取文章列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<MdArticleModel></returns>
    public Page<MdArticleModel> GetMdArticlePageList(MdArticleModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Title))
        {
            sqlWhere += " and `title` like concat('%', @Title, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.SubTitle))
        {
            sqlWhere += " and `sub_title` like concat('%', @SubTitle, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.SeoTitle))
        {
            sqlWhere += " and `seo_title` like concat('%', @SeoTitle, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Author))
        {
            sqlWhere += " and `author` like concat('%', @Author, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.IsExternalLink))
        {
            sqlWhere += " and `is_external_link` = @IsExternalLink ";
        }
        if (model.CategoryId > 0)
        {
            sqlWhere += " and `category_id` = @CategoryId ";
        }
        if (!string.IsNullOrWhiteSpace(model.ArticleType))
        {
            sqlWhere += " and `article_type` = @ArticleType ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }
        sqlWhere += " and `del_flag` = '0' ";
        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过编号更新文章
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>long</returns>
    public long UpdateMdArticleById(MdArticleModel model)
    {
        var sqlSet = string.Empty;

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.Title))
        {
            sqlSet += " `title` = @Title, ";
        }

        if (model.SubTitle != null)
        {
            sqlSet += " `sub_title` = @SubTitle, ";
        }

        if (model.SeoTitle != null)
        {
            sqlSet += " `seo_title` = @SeoTitle, ";
        }

        if (model.SeoDesc != null)
        {
            sqlSet += " `seo_desc` = @SeoDesc, ";
        }

        if (model.SeoKeywork != null)
        {
            sqlSet += " `seo_keywork` = @SeoKeywork, ";
        }

        if (model.Cover != null)
        {
            sqlSet += " `cover` = @Cover, ";
        }

        if (model.ThumCover != null)
        {
            sqlSet += " `thum_cover` = @ThumCover, ";
        }

        if (model.Description != null)
        {
            sqlSet += " `description` = @Description, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Content))
        {
            sqlSet += " `content` = @Content, ";
        }

        if (model.Author != null)
        {
            sqlSet += " `author` = @Author, ";
        }

        if (model.Tag != null)
        {
            sqlSet += " `tag` = @Tag, ";
        }

        if (model.Files != null)
        {
            sqlSet += " `files` = @Files, ";
        }

        if (model.VideoFiles != null)
        {
            sqlSet += " `video_files` = @VideoFiles, ";
        }

        if (model.Link != null)
        {
            sqlSet += " `link` = @Link, ";
        }

        if (!string.IsNullOrWhiteSpace(model.IsExternalLink))
        {
            sqlSet += " `is_external_link` = @IsExternalLink, ";
        }

        if (model.CategoryId > -1)
        {
            sqlSet += " `category_id` = @CategoryId, ";
        }

        if (!string.IsNullOrWhiteSpace(model.ArticleType))
        {
            sqlSet += " `article_type` = @ArticleType, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `del_flag` = '0' and `article_id` = @ArticleId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过编号删除文章
    /// </summary>
    /// <param name="ids">编号多个逗号分隔</param>
    /// <returns>int</returns>
    public int DeleteMdArticleById(string ids)
    {
        string sqlWhere = " and `article_id` in @ArticleIds ";

        return Delete(sqlWhere, new { ArticleIds = ids.Split(',').ToArray() });
    }
}

